import {Component, OnInit ,Input} from '@angular/core';
import { SelectComponent } from './select.component';

@Component({
  selector: 'el-option',
  templateUrl: './option.component.html'
})
export class OptionComponent implements OnInit {
  private rootSelect:any;
    @Input() disabled: boolean = false;
    @Input() value: any;
    @Input() label: string | number = '';
    @Input() rootDisabled: boolean = false;
    @Input() itemSelected: boolean = false;

    constructor(rootSelect: SelectComponent){
        this.rootSelect  = rootSelect;
    };
  

  ngOnInit(): void {
    var _this = this;
    var /** @type {?} */ updateHandle = function () {
        if (_this.rootSelect.multiple) {
            _this.itemSelected = Array.isArray(_this.rootSelect.model) && _this.rootSelect.model.indexOf(_this.value) > -1;
        }
        else {
            _this.itemSelected = _this.value === _this.rootSelect.model;
        }
        _this.itemSelected && _this.rootSelect.changeLabel(_this.label);
    };
    this.rootDisabled = this.rootSelect.disabled;
    updateHandle();
    this.rootSelect.subscriber.push(updateHandle);
    this.rootSelect.appendOptions({
        value: this.value,
        label: this.label,
    });
  }

  clickHandle(event:any){
    event.stopPropagation();
    if (this.disabled || this.rootDisabled)
        return;
    this.rootSelect.changeLabel(this.label, this.value);
  }
  
}
